﻿using M_ApiResult;
using M_Purchase.Api.Appliction.Command.PurchCommand;
using M_Purchase.Domain;
using M_Purchase.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M_Purchase.Api.Controllers
{
    /// <summary>
    /// 采购管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PruchController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public PruchController (IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreatePurch(PurchCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)   
            {
                result.Code = -1;
                result.Message = "添加采购信息失败";
                throw;
            }
            return result;

        }
        /// <summary>
        /// 显示采购信息（入库出库）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Purch>>> GetInList([FromQuery] InListCommand command)
        {
            ApiResult<PageDTO<Purch>> result = new ApiResult<PageDTO<Purch>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据采购编号显示采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Purch>>> GetPuridList([FromQuery] GetPurcehidCommand command)
        {
            ApiResult<List<Purch>> result = new ApiResult<List<Purch>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Purch>>> GetPurchList([FromQuery] PurchListCommand command)
        {
            ApiResult<PageDTO<Purch>> result = new ApiResult<PageDTO<Purch>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示供应商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Supplier>>> GetSuppList()
        {
            ApiResult<List<Supplier>> result = new ApiResult<List<Supplier>>();
            try
            {
                result.Data = await _mediator.Send(new SupplierListDCommand());

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示物流信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Logistics>>> GetLogisticsList([FromQuery]LogisticsListCommand command)
        {
            ApiResult<List<Logistics>> result = new ApiResult<List<Logistics>>();
            try
            {
                result.Data = await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改物流信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateLogistics([FromQuery]LogisticsUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改物流信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除采购信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletePurch([FromQuery] PurchDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "单独删除采购信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填采购数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<Purch>> HandlePurch([FromQuery] PurchGetByidCommand command)
        {
            ApiResult<Purch> result = new ApiResult<Purch>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdatePurch(PurchUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改采购信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 选择供应商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpSupplier([FromQuery]SupplierUpDateIDCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "选择供应商失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据采购ID显示供应商
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<SupDto>>> Handles([FromQuery] SupplierListPurDCommand command)
        {
            ApiResult<List<SupDto>> result = new ApiResult<List<SupDto>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
